﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace NMapper
{
    public static class MappingExtensions
    {
        public static IEnumerable<TDbValue> Select<TDatabase, TDatabaseEntity, TDbValue>(this IDbValueMapping<TDatabase, TDatabaseEntity, TDbValue> mapping, IEnumerable transient)
            where TDatabase : IDisposable
        {
            return transient.Cast<object>().Select(x => (TDbValue)mapping.TransientProperty.GetValue(x, null));
        }

        public static IEnumerable<IGrouping<TDbValue, object>> GroupBy<TDatabase, TDatabaseEntity, TDbValue>(this IDbValueMapping<TDatabase, TDatabaseEntity, TDbValue> mapping, IEnumerable transient)
            where TDatabase : IDisposable
        {
            return transient.Cast<object>().GroupBy(x => (TDbValue)mapping.TransientProperty.GetValue(x, null));
        }

        public static TDbValue Get<TDatabase, TDatabaseEntity, TDbValue>(this IDbValueMapping<TDatabase, TDatabaseEntity, TDbValue> mapping, object obj)
            where TDatabase : IDisposable
        {
            return (TDbValue)mapping.TransientProperty.GetValue(obj, null);
        }
    }
}